﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class DominoInsertViewModel : NotificationObject
    {
        DominoInsert gWindow;
        DominoViewModel gParentViewModel;

        public DominoInsertViewModel(DominoInsert pWindow, DominoViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            List<WDominoType> list = WDominoType.GetAll();
            foreach(var item in list)
            {
                DominoTypes.Add(item.DominoTypeName.Trim());
            }
        }

        private List<String> dominoTypes = new List<String>();
        public List<String> DominoTypes
        {
            get
            {
                return dominoTypes;
            }

            set
            {
                dominoTypes = value;
                RaisePropertyChanged("DominoTypes");
            }
        }

        private String dominoTypesSelected;
        public String DominoTypesSelected
        {
            get
            {
                return dominoTypesSelected;
            }

            set
            {
                dominoTypesSelected = value;
                RaisePropertyChanged("DominoTypesSelected");
            }
        }

        private String dominoStockPrice;
        public String DominoStockPrice
        {
            get
            {
                return dominoStockPrice;
            }
            set
            {
                dominoStockPrice = value;
                RaisePropertyChanged("DominoStockPrice");
            }
        }

        private String dominoSellPrice;
        public String DominoSellPrice
        {
            get
            {
                return dominoSellPrice;
            }
            set
            {
                dominoSellPrice = value;
                RaisePropertyChanged("DominoSellPrice");
            }
        }



        private String addCount;
        public String AddCount
        {
            get
            {
                return addCount;
            }
            set
            {
                addCount = value;
                RaisePropertyChanged("AddCount");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            int dominoTypeUID = WDominoType.GetDominoTypeUID(DominoTypesSelected);
            List<WDomino> list = new List<WDomino>();
            for(int i = 0; i < Convert.ToInt16(AddCount); i++)
            {
                WDomino item = new WDomino();
                item.DominoTypeUID = dominoTypeUID;
                item.DominoStockPrice = Convert.ToSingle(DominoStockPrice);
                item.DominoSellPrice = Convert.ToSingle(DominoSellPrice);
                item.IsSold = 0;
                list.Add(item);
            }
            HttpResponse response = WDomino.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
